package com.gitee.util;

import com.gitee.poto.Article;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

public class ExcelUtil {

    //读取exel,将文件内容打印出来
    public static void main(String[] args) {
        List<Article> exceInfo = getExceInfo();
    }

    public static List<Article> getExceInfo() {
        ArrayList<Article> arrayList = new ArrayList<>();
        try {
            FileInputStream fileInputStream = new FileInputStream("E:\\34期学过视频\\DSJ06实时第一阶段\\day06_ELK02\\资料\\数据集\\baijia.xlsx");
            //获取我们解析excel表格的对象
            XSSFWorkbook xssfSheets = new XSSFWorkbook(fileInputStream);
            //获取excel的第一个sheet页
            XSSFSheet sheetAt = xssfSheets.getSheetAt(0);
            //获取我们sheet页的最后一行的数字之，说白了就是看这个excel一共有多少行
            int lastRowNum = sheetAt.getLastRowNum();
            for (int i = 0; i < lastRowNum; i++) {
                Article article = new Article();
                //获取一行数据
                XSSFRow row = sheetAt.getRow(i);
                //通过row对象,解析里面的每一个字段
                XSSFCell title = row.getCell(0);
                XSSFCell from  = row.getCell(1);
                XSSFCell time = row.getCell(2);
                XSSFCell readCount = row.getCell(3);
                XSSFCell content = row.getCell(4);
//                System.out.println(title.toString());
                article.setId(i+"");
                article.setTitle(title.toString());
                article.setContent(content.toString());
                article.setFrom(from.toString());
                article.setReadCounts(readCount.toString());
                article.setTimes(time.toString());
                arrayList.add(article);
                fileInputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return arrayList;
        }
    }
}
